~ chicken-core (master) /manual/Module (chicken read-syntax)


  1[[tags: manual]]
  2[[toc:]]
  3
  4== Module (chicken read-syntax)
  5
  6This module provides procedures which can be used to extend the reader
  7with custom read syntax. When using the interpreter, these procedures
  8take immediate effect. During compilation, however, these procedures
  9might need to be evaluated before the source code is read for compilation. You can use
 10the {{-X}} option when invoking {{csc}} to load source or compiled code
 11to make sure your read-syntax extensions are available.
 12
 13=== define-reader-ctor
 14
 15<procedure>(define-reader-ctor SYMBOL PROC)</procedure>
 16
 17Define new read-time constructor for {{#,}} read syntax. For further information, see
 18the documentation for [[http://srfi.schemers.org/srfi-10/srfi-10.html|SRFI-10]].
 19
 20
 21=== set-read-syntax!
 22
 23<procedure>(set-read-syntax! CHAR-OR-SYMBOL PROC)</procedure>
 24
 25When the reader encounters the non-whitespace character {{CHAR}} while reading
 26an expression from a given port, then the procedure {{PROC}} will be called with
 27that port as its argument. The procedure should return a value that will be returned
 28to the reader:
 29
 30<enscript highlight=scheme>
 31 ; A simple RGB color syntax:
 32
 33 (set-read-syntax! #\%
 34   (lambda (port)
 35     (apply vector
 36       (map (cut string->number <> 16)
 37	    (string-chop (read-string 6 port) 2) ) ) ) )
 38
 39 (with-input-from-string "(1 2 %f0f0f0 3)" read)
 40 ; ==> (1 2 #(240 240 240) 3)
 41</enscript>
 42
 43If {{CHAR-OR-SYMBOL}} is a symbol, then a so-called ''read-mark'' handler is defined.
 44In that case the handler procedure will be called when a character-sequence of the
 45form {{#!SYMBOL}} is encountered.
 46
 47You can undo special handling of read-syntax by passing {{#f}} as the second argument
 48(if the syntax was previously defined via {{set-read-syntax!}}).
 49
 50As a special case, your handler can return zero values, via {{(values)}}.  This causes
 51the reader to completely ignore whatever input you've read, rather than returning some
 52possibly unspecified value.  This can be useful in macro context, reading comments,
 53conditional compilation, and so forth.  Available in CHICKEN 4.6.6 and later.
 54
 55Note that all of CHICKEN's special non-standard read-syntax is handled directly by the reader.
 56To disable built-in read-syntax, define a handler that triggers an error (for example).
 57
 58
 59=== set-sharp-read-syntax!
 60
 61<procedure>(set-sharp-read-syntax! CHAR-OR-SYMBOL PROC)</procedure>
 62
 63Similar to {{set-read-syntax!}}, but allows defining new {{#<CHAR> ...}} reader syntax.
 64If the first argument is a symbol, then this procedure is equivalent to {{set-read-syntax!}}.
 65
 66{{PROC}} may be {{#f}} to disable previously defined "sharp" read syntax.
 67
 68
 69=== set-parameterized-read-syntax!
 70
 71<procedure>(set-parameterized-read-syntax! CHAR-OR-SYMBOL PROC)</procedure>
 72
 73Similar to {{set-sharp-read-syntax!}}, but intended for defining reader syntax of the
 74form {{#<NUMBER><CHAR> ...}}. The handler procedure {{PROC}} will be called with two
 75arguments: the input port and the number preceding
 76the dispatching character.
 77If the first argument is a symbol, then this procedure is equivalent to {{set-read-syntax!}}.
 78
 79{{PROC}} may be {{#f}} to disable previously defined parameterized read syntax.
 80
 81
 82=== copy-read-table
 83
 84<procedure>(copy-read-table READ-TABLE)</procedure>
 85
 86Returns a copy of the given read-table. You can access the currently
 87active read-table with {{(current-read-table)}}.  This procedure can
 88be useful to restore an old read-table after temporarily introducing
 89new read syntax.
 90
 91
 92=== current-read-table
 93
 94<parameter>(current-read-table)</parameter>
 95
 96A read-table object that holds read-procedures for special non-standard
 97read-syntax (see {{set-read-syntax!}} for more information).
 98
 99
100---
101Previous: [[Module (chicken random)]]
102
103Next: [[Module (chicken repl)]]
Trap